home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 June / EnigmA AMIGA RUN 19 (1997)(G.R. Edizioni)(IT)[!][issue 1997-06][EAR-CD III].iso / recent1 / apic1805.lha / APIC / examples / Amikeyterm.asm next >
Assembly Source File  |  1997-05-05  |  6KB  |  424 lines

  1.  
  2.  
  3. ;PIC16C84 keybord controller for Amiga keybords
  4. ;pressed keys are located in table and send in serial ascii format (19200baud)
  5. ;
  6. ;Dirk Duesterberg duesterb@unixserv.rz.fh-hannover.de
  7. ;                 http://linux.rz.fh-hannover.de/~duesterb
  8.  
  9.  
  10.     list    p=PIC16C84, r=dec, s=off
  11.  
  12.     include    "ram:p16cxx.inc"
  13.  
  14.  
  15. #define    ACLK    PortA,0
  16. #define    ADAT    PortA,1
  17.  
  18. #define    TXD    PortA,2
  19. #define c    3,0
  20. #define z    3,2
  21.  
  22. RA    =    5        ;PortA is register 5
  23.  
  24.  
  25.  
  26.  
  27.     CBLOCK    0ch
  28.  
  29.        count0
  30.        count1
  31.        Akeydat
  32.        serbuf
  33.  
  34.     ENDC
  35.  
  36.  
  37.  
  38.     movlw    11111011b        ;pin 2 is output (TXD)
  39.     tris    PortA
  40.  
  41.     movlw    0            ;PortB is output
  42.     tris    PortB
  43.  
  44.     goto    Reset
  45.  
  46.  
  47.  
  48. rawkeys    movf    Akeydat,w
  49.     andlw    7fh            ;clr bit 7 (make/break or pressed/unpressed)
  50.     addwf    pcl,f
  51.  
  52.  
  53. ; characters, numbers and spezial keys
  54. ; $00-$3F
  55.  
  56.     retlw    00h            ;00h
  57.     retlw    00h            ;01h
  58.     retlw    00h            ;02h
  59.     retlw    00h            ;03h
  60.     retlw    00h            ;04h
  61.     retlw    00h            ;05h
  62.     retlw    00h            ;06h
  63.     retlw    00h            ;07h
  64.  
  65.     retlw    00h            ;08h
  66.     retlw    00h            ;09h
  67.     retlw    00h            ;0Ah
  68.     retlw    00h            ;0Bh
  69.     retlw    00h            ;0Ch
  70.     retlw    00h            ;0Dh
  71.     retlw    00h            ;0Eh    
  72.     retlw    00h            ;0Fh
  73.  
  74.  
  75.  
  76.     retlw    "q"            ;10h
  77.     retlw    "w"            ;11h
  78.     retlw    "e"            ;12h
  79.     retlw    "r"            ;13h
  80.     retlw    "t"            ;14h
  81.     retlw    "z"            ;15h
  82.     retlw    "u"            ;16h
  83.     retlw    "i"            ;17h
  84.  
  85.     retlw    "o"            ;18h
  86.     retlw    "p"            ;19h
  87.     retlw    "ü"            ;1Ah
  88.     retlw    "+"            ;1Bh
  89.     retlw    00h            ;1Ch
  90.     retlw    00h            ;1Dh
  91.     retlw    00h            ;1Eh
  92.     retlw    00h            ;1Fh
  93.  
  94.  
  95.  
  96.     retlw    "a"            ;20h
  97.     retlw    "s"            ;21h
  98.     retlw    "d"            ;22h
  99.     retlw    "f"            ;23h
  100.     retlw    "g"            ;24h
  101.     retlw    "h"            ;25h
  102.     retlw    "j"            ;26h
  103.     retlw    "k"            ;27h
  104.  
  105.     retlw    "l"            ;28h
  106.     retlw    "ö"            ;29h
  107.     retlw    "ä"            ;2Ah
  108.     retlw    00h            ;2Bh
  109.     retlw    00h            ;2Ch
  110.     retlw    00h            ;2Dh
  111.     retlw    00h            ;2Eh
  112.     retlw    00h            ;2Fh
  113.  
  114.  
  115.  
  116.     retlw    00h            ;30h
  117.     retlw    00h            ;31h
  118.     retlw    00h            ;32h
  119.     retlw    00h            ;33h
  120.     retlw    00h            ;34h
  121.     retlw    00h            ;35h
  122.     retlw    00h            ;36h
  123.     retlw    00h            ;37h
  124.  
  125.     retlw    00h            ;38h
  126.     retlw    00h            ;39h
  127.     retlw    00h            ;3Ah
  128.     retlw    00h            ;3Bh
  129.     retlw    00h            ;3Ch
  130.     retlw    00h            ;3Dh
  131.     retlw    00h            ;3Eh
  132.     retlw    00h            ;3Fh
  133.  
  134. ; other spezial keys (space, TAB, Return)
  135. ; $40-$4F
  136.  
  137.     retlw    00h            ;40h
  138.     retlw    00h            ;41h
  139.     retlw    00h            ;42h
  140.     retlw    00h            ;43h
  141.     retlw    00h            ;44h
  142.     retlw    00h            ;45h
  143.     retlw    00h            ;46h
  144.     retlw    00h            ;47h
  145.  
  146.     retlw    00h            ;48h
  147.     retlw    00h            ;49h
  148.     retlw    00h            ;4Ah
  149.     retlw    00h            ;4Bh
  150.     retlw    00h            ;4Ch
  151.     retlw    00h            ;4Dh
  152.     retlw    00h            ;4Eh
  153.     retlw    00h            ;4Fh
  154.  
  155.  
  156. ; Function keys, Help etc.
  157. ; $50-$5F
  158.  
  159.     retlw    00h            ;50h
  160.     retlw    00h            ;51h
  161.     retlw    00h            ;52h
  162.     retlw    00h            ;53h
  163.     retlw    00h            ;54h
  164.     retlw    00h            ;55h
  165.     retlw    00h            ;56h
  166.     retlw    00h            ;57h
  167.  
  168.     retlw    00h            ;58h
  169.     retlw    00h            ;59h
  170.     retlw    00h            ;5Ah
  171.     retlw    00h            ;5Bh
  172.     retlw    00h            ;5Ch
  173.     retlw    00h            ;5Dh
  174.     retlw    00h            ;5Eh
  175.     retlw    00h            ;5Fh
  176.  
  177.  
  178.  
  179. ; shifting keys like shift, amiga, Alternate and Control
  180. ; $60-6F
  181.     
  182.     retlw    00h            ;60h
  183.     retlw    00h            ;61h
  184.     retlw    00h            ;62h
  185.     retlw    00h            ;63h
  186.     retlw    00h            ;64h
  187.     retlw    00h            ;65h
  188.     retlw    00h            ;66h
  189.     retlw    00h            ;67h
  190.  
  191.     retlw    00h            ;68h
  192.     retlw    00h            ;69h
  193.     retlw    00h            ;6Ah
  194.     retlw    00h            ;6Bh
  195.     retlw    00h            ;6Ch
  196.     retlw    00h            ;6Dh
  197.     retlw    00h            ;6Eh
  198.     retlw    00h            ;6Fh
  199.  
  200.  
  201. ;spezial keybord commandos
  202. ; $70-$7F
  203.  
  204.     retlw    00h            ;70h    
  205.     retlw    00h            ;71h
  206.     retlw    00h            ;72h
  207.     retlw    00h            ;73h
  208.     retlw    00h            ;74h
  209.     retlw    00h            ;75h
  210.     retlw    00h            ;76h
  211.     retlw    00h            ;77h
  212.  
  213.     retlw    00h            ;78h
  214.     retlw    00h            ;79h=F9h= letzter tasten code war fehlerhaft
  215.     retlw    00h            ;7Ah=FAh= tastenpuffer im keybord voll
  216.     retlw    00h            ;7Bh
  217.     retlw    00h            ;7Ch=FCh= selbsttest der tastatur war fehlerhaft
  218.     retlw    00h            ;7Dh=FDh= beginn der beim Einschalten gedrueckten Tasten
  219.     retlw    00h            ;7Eh=FEh= ende der beim Einschalten gedrueckten Tasten
  220.     retlw    00h            ;7Fh
  221.  
  222.     
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. Reset    clrf    Akeydat            ;Akeydat is used as character counter
  246.  
  247. check    movlw    11
  248.     subwf    Akeydat,w        ;how many characters ?
  249.     btfsc    z
  250.     goto    ready
  251.  
  252.     call    dat
  253.     movwf    serbuf
  254.     call    sendb            ;send letter
  255.     incf    Akeydat,f
  256.     goto    check
  257.  
  258.  
  259.  
  260. dat    movf    Akeydat,w        ;letter to w routine
  261.     addwf    pcl,f            ;jump in table
  262.     retlw    "mache RESET"
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. ready    call    sync
  271.  
  272.  
  273.  
  274.  
  275. rcAdat    movlw    7
  276.     movwf    count0
  277.  
  278. :loop    call    wACLK            ;wait for Amiga CLK
  279.     rlf    Akeydat,f        ;rotate bits into register
  280.     decfsz    count0,f
  281.     goto    :loop            ;format is x6543210
  282.  
  283.  
  284.     rlf    Akeydat,f        ;format is 6543210x
  285.     call    wACLK            ;wait for Amiga CLK
  286.     rrf    Akeydat,f        ;format is 76543210, jippije
  287.     comf    Akeydat,f        ;data is inverted
  288.  
  289.     movlw    5
  290.     call    wms            ;wait 5 ms
  291.  
  292. ;    mov    serbuf,Akeydat
  293. ;    call    sendb            ;send raw data
  294.  
  295.  
  296.     call    rawkeys
  297.     movwf    serbuf
  298.  
  299.     btfss    Akeydat,7        ;no sending if key up flag is set
  300.     call    sendb            ;send the in table found code
  301.  
  302.     call    AHshake            ;all data OK, do the Handshake
  303.     goto    rcAdat            ;receive next byte from keybord
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. sync    call    wACLK            ;wait for clock and do no Acknowledge
  316.     
  317.     movlw    250
  318.     call    wms            ;wait 250 ms (we want the sync mode!)
  319.  
  320.     call    wACLK            ;wait for Amiga CLK for Handshake
  321.  
  322.     movlw    1
  323.     call    wms            ;wait 1 ms
  324.  
  325.     call    AHshake            ;now we do the shake!
  326.     return
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333. wms    movwf    count0
  334.  
  335. :loop    movlw    248
  336.     movwf    count1
  337. :do_it    nop
  338.  
  339.     decfsz    count1,f
  340.     goto    :do_it
  341.  
  342.     decfsz    count0,f
  343.     goto    :loop
  344.  
  345.     return
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352. AHshake    movlw    11111001b        ;bit1 = ADAT = output
  353.     tris    RA
  354.  
  355.     bcf    ADAT            ;clr Amiga data line
  356.     
  357.     movlw    40
  358.     movwf    count0            ;40 * 3 = 120cycles = 120 µs (min 75µs)
  359. :do_it    decfsz    count0,f
  360.     goto    :do_it
  361.  
  362.     movlw    11111011b        ;bit1 = ADAT = input
  363.     tris    RA
  364.     return
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371. wACLK    btfsc    ACLK            ;wait for neg clock pulse
  372.     goto    wACLK
  373.  
  374.     btfss    Adat
  375.     bcf    c
  376.     btfsc    Adat
  377.     bsf    c            ;mov the data to carry bit
  378.  
  379. wACLK2    btfss    ACLK            ;wait for pos clock pulse
  380.     goto    wACLK2
  381.     return
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389. sendb    call    wbit            ;this are stop bits from previous sending
  390.     call    wbit
  391.  
  392.     bsf    TXD            ;send startbit
  393.     movlw    8
  394.     movwf    count0            ;8 bits to send
  395.     comf    serbuf,f        ;invert serbuf
  396.  
  397. s_it    call    wbit
  398.     rrf    serbuf,f
  399.  
  400.     btfss    c
  401.     bcf    TXD
  402.     btfsc    c
  403.     bsf    TXD
  404.  
  405.     decfsz    count0,f
  406.     goto    s_it            ;all bits send ? decrement the bitcounter
  407.  
  408.     call    wbit
  409.     bcf    TXD            ;clear TXD, stopbit, lenght is defined by 
  410.                     ;next sending
  411.     return
  412.  
  413.  
  414.  
  415.  
  416.  
  417. wbit    movlw    13
  418.     movwf    count1            ;19200 at 4 Mhz
  419. :loop    decfsz    count1,f
  420.     goto    :loop
  421.     nop
  422.     return
  423.  
  424.